from flask import Flask,g
from .config import Config
from .exts import db,migrate,mail
from app.models import *
import os
from flask_login import LoginManager

def create_app(config_class=Config):
    app = Flask(__name__)

    # 加载配置
    app.config.from_object(config_class)

    # 初始化扩展
    db.init_app(app)
    migrate.init_app(app,db)
    mail.init_app(app)
    # login_manager.init_app(app)
    # login_manager.login_view = 'auth.login'  # 登录视图端点名
    # 注册蓝图
    from .blueprint import auth_bp, blog_bp, comment_bp, security_bp
    app.register_blueprint(auth_bp)
    app.register_blueprint(blog_bp)
    app.register_blueprint(comment_bp)
    app.register_blueprint(security_bp)


    # 初始化数据库（用于命令行操作）
    with app.app_context():
        db.create_all()

    return app